﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Barebones.Framework;

namespace Barebones.Components
{
    public abstract class EngineComponent : IComponent, IDisposable
    {
        public Engine Owner
        { get; private set; }

        public abstract IEnumerable<Dependencies.IDependency> GetDependencies();

        protected virtual void OnOwnerSet()
        { }

        protected virtual void OnOwnerRemoved(Engine owner)
        { }

        protected virtual void OnDisposed()
        { }

        internal void SetOwner(Engine owner)
        {
            if (Owner != null)
                OnOwnerRemoved(Owner);

            Owner = owner;

            if (Owner != null)
                OnOwnerSet();
        }

        public void Dispose()
        {
            OnDisposed();

            if (Owner == null)
                return;

            Owner.RemoveComponent(this);
        }
    }
}
